#include <stdio.h>          //printf
#include <iostream>         //cout, endl,
#include <unistd.h>         //usleep,
#include <bits/stdc++.h>    //strcpy,
#include "HCNetSDK.h"
#include "ptz.hpp"
using namespace std;

void CALLBACK g_ExceptionCallBack(DWORD dwType, LONG lUserID, LONG lHandle, void *pUser)
{
    // char tempbuf[256] = {0};
    switch(dwType) 
    {
    case EXCEPTION_RECONNECT:    //预览时重连
        printf("----------reconnect--------%ld\n", time(NULL));
    break;
    default:
    break;
    }
}



int main(int argc, char *argv[]) {
    // 获取命令行参数
    PTZHelper helper(argc, argv);
    int dir = helper.director();
    int channel = helper.channel();
    const char* nvr_ip = helper.get_opt_value('r');
    const char* username = helper.get_opt_value('u');
    const char* password = helper.get_opt_value('p');

    //初始化Hikvision Network SDK
    NET_DVR_Init();
    char log_dir[] = "./sdkLog";
    NET_DVR_SetLogToFile(0, log_dir);
    //设置连接时间与重连时间
    NET_DVR_SetConnectTime(2000, 1);
    NET_DVR_SetReconnect(10000, true);

    //设置异常消息回调函数
    NET_DVR_SetExceptionCallBack_V30(0, NULL,g_ExceptionCallBack, NULL);
    // 注册设备
    LONG lUserID;

    //登录参数，包括设备地址、登录用户、密码等
    NET_DVR_USER_LOGIN_INFO struLoginInfo = {0};
    struLoginInfo.bUseAsynLogin = 0; //同步登录方式
    strcpy(struLoginInfo.sDeviceAddress, nvr_ip); //设备IP地址
    struLoginInfo.wPort = 8000; //设备服务端口
    strcpy(struLoginInfo.sUserName, username); //设备登录用户名
    strcpy(struLoginInfo.sPassword, password); //设备登录密码

    //设备信息, 输出参数
    NET_DVR_DEVICEINFO_V40 struDeviceInfoV40 = {0};

    lUserID = NET_DVR_Login_V40(&struLoginInfo, &struDeviceInfoV40);
    if (lUserID < 0)
    {
        printf("Login failed, error code: %d\n", NET_DVR_GetLastError());
        NET_DVR_Cleanup();
        return -1;
    }

    int iChannel = channel;//设备通道号
    DWORD dwPTZCommand = dir; //移动方向
    DWORD dwStop = 0; //开始转动
    if(!NET_DVR_PTZControl_Other(lUserID, iChannel, dwPTZCommand, dwStop))
    {
        printf("PAN_RIGHT start failed, error code: %d\n", NET_DVR_GetLastError());
        NET_DVR_Logout(lUserID);
        NET_DVR_Cleanup();
        return -1;
    }

    //云台控制接口下发命令之后是云台持续运动的，持续转动5秒之后再调用接口下发停止转动命令
    usleep(500000);
    dwStop = 1; //停止转动
    if(!NET_DVR_PTZControl_Other(lUserID, iChannel, dwPTZCommand, dwStop))
    {
        printf("PAN_RIGHT stop failed, error code: %d\n", NET_DVR_GetLastError());
    }

    //注销用户
    NET_DVR_Logout(lUserID);
    //释放SDK资源
    NET_DVR_Cleanup();

    return 0;
}

